package core

import java.beans.XMLDecoder;

class PersonController {

	def commonService
    def index() {
		def out = "<pre>"
		
//		def p
//		def p = Person.get(1) // get person with id == 1
		// check the log to see of the database is accesses
//		def p = Person.read(1) // same as get, but read only
//		def p = Person.load(1) // no database access until a method is called on p
		// other than getId()
		// check the log to see of the database is accesses
//		println("Person Id : ${p.id}")
		// check the log to see of the database is accesses
//		println("Person First Name: ${p.firstName}")
		// check the log to see of the database is accesses

//		new Car(engine:new Engine()).save()
//		def c = Car.get(2)
//		c.delete() // both Car and Engine deleted
//		new Engine(car:new Car()).save() // will cause an error
		
		// Saves Author and books
//		new Author(name:"Stephen King")
//		.addToBooks(new Book(title:"The Stand"))
//		.addToBooks(new Book(title:"The Shining"))
//		.save()
		
		def query
		
		/*query= Person.where {
			firstName == "Raed" && lastName == "Hindaileh"
		}		
		out += "\n find : " + q.find()
		
		query.findAll().each {
			out += "\n findAll : " + it
		}
		
		query.list(sort:'lastName').each {
			out += "\n list : " + it 
		}
		
		query = Person.where {
			(lastName == "Hindaileh" && firstName != "Fred") || ( birthDate >= new Date("1990/01/01") )
		}
		
		query.list(sort:'lastName').each {
			out += "\n list comp : " + it
		}
		
		query = Person.where {
			lastName =~ "%ai%"
		}
		
		query.list(sort:'lastName').each {  out += "\n reg : " + it }
		
		def hindailehs = Person.where {
			lastName == "Hindaileh"
		}
		def raedHindaileh = hindailehs.where {
			firstName == "Raed"
	   	}
	   
	   out += "\n composition : " + raedHindaileh.find()

	   	query = Person.where {
		   legalIdentifiers { idValue == "88775544" } || firstName =~ "%ae%"
		}
	  
	  	query.list(sort:"firstName").each { out += "\n Associations: " + it }
	  
	   
		 
		 query = Person.where {
			 legalIdentifiers.size() == 0
		  }
		
			query.list(sort:"firstName").each { out += "\n Associations: " + it + " ${it.age()} " }
	   */
		
		/*
		query = Person.where {
		    lastName == 'Hindaileh'
		}
		//int total = query.updateAll(lastName:"Hindaileh")

		
		query.list().each { out += "\n Associations: ${query.deleteAll()}" + it  } 
		out += DocumentType.list(max:1,offset:new Random().nextInt(3))[0].name	
		
		
		query = Person.where {
			 legalIdentifiers.size() <= 2
		  }
		*/
		
		//query.list(sort:"firstName").each { out += "\n Associations: " + it + " ${it.age} " }
		
		
		out += "</pre>"
		render text: "Peroson.index \n ${out}"
	}
}
